import util from '../../utils/util';

const app = getApp()

Page({
    data: {
		TabCur: 1,
		scrollLeft:0,
		applyList:[],
		index:0,
		stuno:'',
		applyId:0
    },
    tabSelect(e) {
		var curTab = e.currentTarget.dataset.id;
		this.setData({
		  TabCur: curTab,
		  scrollLeft: (e.currentTarget.dataset.id-1)*60
		});
		if(curTab == 2) {
			this.loadApplyInfos();
		}
    },
    onLoad() {
		this.loadApplyInfos();
	},
	onShow(){
	  var that = this;
	  this.loadApplyList();
	  //验证码
	  this.createCode();
	},
	//验证码切换
	nextCode(){
	  this.createCode()
	},
	//创建验证码
	createCode() {
	  var code;
	  //首先默认code为空字符串
	  code = '';
	  //设置长度，这里看需求，我这里设置了4
	  var codeLength = 4;
	  //设置随机字符
	  var random = new Array(0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z','a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z');
	  //循环codeLength 我设置的4就是循环4次
	  for (var i = 0; i < codeLength; i++) {
	    //设置随机数范围,这设置为0 ~ 36
	    var index = Math.floor(Math.random() * 62);
	    //字符串拼接 将每次随机的字符 进行拼接
	    code += random[index];
	  }
	  //将拼接好的字符串赋值给展示的code
	  this.setData({
	    captchacode: code
	  })
	},
	loadApplyList() {
		var that = this;
		util.get(`aftserv/listApply`,response=>{
		  // TIPS: 为0不一般是失败么
		  if (response.code == 0) {
		      that.setData({
				  applyList: response.data || []
			  });
		  } else {
			  wx.showToast({
			    title: response.msg,
			    icon: 'none',
			    duration: 2000
			  }) 
		  }
		})
	},
	monthChange(e) {
		var applyId = this.data.applyList[e.detail.value].id;
		console.log('picker发 送选择改变，携带值为', applyId);
		this.setData({
			applyId: applyId,
			index: e.detail.value
		})
  },
  bindStunoInput: function (e) {
      this.setData({
        stuno: e.detail.value
      })
  },
  selectStudent(){
	  var that = this;
	  var stuno = that.data.stuno;
	  if(stuno) {
		  util.get(`aftserv/getStuByStuno?stuno=${stuno}`,response=>{
		    // TIPS: 为0不一般是失败么
		    if (response.code == 0) {
				if(response.data) {
					var stuObj = response.data;
					that.setData({
					  stuid: stuObj.id,
					  stuname: stuObj.stuname,
					  stusex: stuObj.stusex,
					  gradeClass: stuObj.gradeClass.gradeNum + '年' + stuObj.gradeClass.classNum + '班'
					});
					console.log('data值：', that.data);
				} else {
					wx.showToast({
					  title: '学生不存在！',
					  icon: 'none',
					  duration: 2000
					})
				}
		        
		    } else {
		  	  wx.showToast({
		  	    title: response.msg,
		  	    icon: 'none',
		  	    duration: 2000
		  	  }) 
		    }
		  })
	  } else {
		  wx.showToast({
		    title: '请输入学号！',
		    icon: 'none',
		    duration: 2000
		  }) 
	  }
	  
  },
  //提交报名
  confrimApply:function(e){
    wx.showLoading({title: '请稍后...'})
    var that = this;
    const {applyId, stuno, stuid} = that.data;
	var inputcaptchacode = e.detail.value.inputcaptchacode;
    if (applyId == null || applyId == 0) {
      wx.showToast({
        title: '请选择报名月份',
        icon: 'error', 
        duration: 2000
      })
      return false;
    }
	if (stuid == null || stuid == 0) {
      wx.showToast({
        title: '请查询学生信息',
        icon: 'error', 
        duration: 2000
      })
      return false;
    }
    if(inputcaptchacode.length==0){
      wx.showToast({
        title: '请输入验证码',
        icon: 'error',
        duration: 2000
      })
      return false;
    }
    if(('' + inputcaptchacode).toUpperCase()!=this.data.captchacode.toUpperCase()){
      wx.showToast({
        title: '验证码不正确',
        icon: 'error',
        duration: 2000
      })
      this.nextCode();
    }else{
      let params = {};
      params.applyId = applyId; //
      params.stuId = stuid; //
	  params.stuno = stuno;
      util.post('aftserv/addApply',params,response=>{
        const data = response;
        if (data.code === 0) {
          console.log("----报名成功----",data)
          //TODO 可以把报名完的数据（applyId-stuid）保存到这里，下次检验一下就行了
          //wx.setStorageSync('token', data.data.openId);
          wx.hideLoading() //关闭提示框
          wx.showToast({
            title: '报名成功！',
            icon: 'scuess', 
            duration: 2000
          })
        }else{
          wx.showToast({
            title: data.msg,
            icon: 'none',
            duration: 2000
          })
        }
      })
  	}
  },
  loadApplyInfos() {
	  var that = this;
	  util.get(`aftserv/listApplyInfo?stuId=`+wx.getStorageSync("stuid"),response=>{
	    // TIPS: 为0不一般是失败么
	    if (response.code == 0) {
	        that.setData({
	  		  applyInfos: response.data || []
	  	  });
	    } else {
	  	  wx.showToast({
	  	    title: response.msg,
	  	    icon: 'none',
	  	    duration: 2000
	  	  }) 
	    }
	  })
  }
})
